home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch3B / Timer.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.4 KB  |  102 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////
  22. // Timer.C: A clock class for the stopwatch
  23. ///////////////////////////////////////////////////
  24. #include "Timer.h"
  25. #include <Xm/Xm.h>
  26.  
  27. Timer::Timer ( XtAppContext  app, 
  28.           TimerCallback callback, 
  29.           int           interval, 
  30.           void         *data )
  31. {
  32.     _func     = callback;
  33.     _data     = data;
  34.     _id       = NULL;
  35.     _app      = app;
  36.     _counter  = 0;
  37.     _interval = interval;
  38. }
  39.  
  40. void Timer::tick()
  41. {
  42.     _counter++;  // Increment a counter for each tick
  43.     
  44.     // Compute the time in seconds and update the Face object
  45.     
  46.     if ( _func )
  47.     (*_func)( elapsedTime(), _data);
  48.     
  49.     // Reinstall the timeout callback
  50.     
  51.     _id = XtAppAddTimeOut ( _app, 
  52.                _interval, 
  53.                &Timer::tickCallback, 
  54.                (XtPointer) this );
  55. }
  56.  
  57.  
  58. void Timer::start()
  59. {
  60.     // Reset the elapsed time
  61.     
  62.     _counter = 0;
  63.     
  64.     if ( _id ) // If a previous callback is still in effect, remove it
  65.     {
  66.     XtRemoveTimeOut ( _id );
  67.     _id = NULL;
  68.     }
  69.     
  70.     // Register a function to be called in _interval milliseconds
  71.     
  72.     _id = XtAppAddTimeOut ( _app, 
  73.                _interval, 
  74.                &Timer::tickCallback, 
  75.                (XtPointer) this );
  76. }
  77.  
  78. void Timer::stop()
  79. {
  80.     // Remove the current timeout function, if any
  81.     
  82.     if ( _id )
  83.     XtRemoveTimeOut ( _id );
  84.     
  85.     _id = NULL;
  86. }
  87.  
  88. void Timer::tickCallback ( XtPointer clientData, XtIntervalId * )
  89. {
  90.     // Get the object pointer and call the corresponding tick function
  91.     
  92.     Timer *obj = (Timer *) clientData;
  93.     
  94.     obj->tick();
  95. }
  96.  
  97. float Timer::elapsedTime()
  98. {
  99.     return ( (float) _counter * _interval / 1000.0 );
  100. }
  101.  
  102.